package com.xuerhua.yxt.view;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.model.TermInfo;
import com.xuerhua.yxt.wechat.utils.WechatGlobalMap;

public class TermView extends TermInfo {
	
	private static final long serialVersionUID = 2414849224346649L;
	
	//年级
	private Map<Integer, String> gradeMap;
	//毕业年
	private Integer finishYear;
	
	private Integer termKey;

	public Map<Integer, String> getGradeMap() {
		return gradeMap;
	}

	public void setGradeMap(Map<Integer, String> gradeMap) {
		this.gradeMap = gradeMap;
	}

	public static String getGradeName(Integer grade) {
		TermView view = (TermView) WechatGlobalMap.get(Const.CACHE_TERM).getValue();
		String gradeName = view.getGradeMap().get(grade);
		if(StringUtils.isBlank(gradeName)) {
			return "已毕业" + grade + "级";
		}
		return gradeName;
	}
	
	public static String getTeacherGradeName(Integer grade) {
		TermView view = (TermView) WechatGlobalMap.get(Const.CACHE_TERM).getValue();
		String gradeName = view.getGradeMap().get(grade);
		if(StringUtils.isBlank(gradeName)) {
			return null;
		}
		return gradeName;
	}
	
	public Integer getFinishYear() {
		if(finishYear == null) {
			init();
		}
		return finishYear;
	}

	public void setFinishYear(Integer finishYear) {
		this.finishYear = finishYear;
	}

	public Integer getTermKey() {
		return termKey;
	}

	public void setTermKey(Integer termKey) {
		this.termKey = termKey;
	}
	public void init() {
		String termName = super.getTermName();
		Integer grade = Integer.valueOf(termName.substring(0, termName.indexOf("-")));
		this.termKey = grade;
		this.finishYear = grade - 6;
		if(this.gradeMap == null) {
			this.gradeMap = getGradeMap(grade);
		}
	}
	
	public static Map<Integer, String> getGradeMap(Integer termYear) {
		Map<Integer, String> map = new LinkedHashMap<>();
		map.put(termYear, "一年级");
		map.put(termYear.intValue() - 1, "二年级");
		map.put(termYear.intValue() - 2, "三年级");
		map.put(termYear.intValue() - 3, "四年级");
		map.put(termYear.intValue() - 4, "五年级");
		map.put(termYear.intValue() - 5, "六年级");
		return map;
	}

}
